<?php
/**模型表模型
 * @Author: 976123967@qq.com
 * @Date:   2014-09-22 22:23:55
 * @Last Modified by:   chen
 * @Last Modified time: 2014-10-22 23:02:38
 */
class ModelModel extends Model{
	protected $tableName = 'model';
	public $model;

	/**
	 * [_initialize 初始化]
	 * @return [type] [description]
	 */
	public function _initialize()
	{
		$this->model=S('model');
	}
	
	/**
	 * [update_cache 模型缓存]
	 * @return [type] [description]
	 */
	public function update_cache()
	{
		$model = $this->order('mid asc')->select();
		$temp =array();
		if($model)
		{
			foreach($model as $k=>$v)
			{
				$temp[$v['mid']]=$v;
			}
		}
		S('model',$temp);
	}



	/**
	 * [$_validate 自动验证]
	 * @var array
	 */
	public $_validate = array(
		array('remark','require','请输入模型名称',1,'regex',3),
		array('name','require','请输入表名称',1,'regex',3),
		array('name','/^[a-z][a-z0-9]*$/i','表名称必须以字母开头并且只能包含字母或者数字',1,'regex',3),
		array('name','_name','表已经存在','1','callback','3'),
	);



	/**
	 *  array(填充字段,填充内容,[填充条件,附加规则])
	 *  填充条件
	 *  Model:: MODEL_INSERT或者1 新增数据的时候处理（默认）
	 *  Model:: MODEL_UPDATE或者2更新数据的时候处理
	 *  Model:: MODEL_BOTH或者3所有情况都进行处理
	 * [$_auto 自动完成]
	 * @var array
	 */
	public $_auto = array(
		array('name','strtolower',3,'function'),
	);


	/**
	 * [_name 验证表是否已经存在]
	 * @param  [type] $con [description]
	 * @return [type]      [description]
	 */
	public function _name($con)
	{
		$mid = I('post.mid');
		if($mid)
			$where['mid'] =  array('neq',$mid);
		$where['name'] = strtolower($con);
		$sta = $this->where($where)->getField('mid');
		if($sta)
		{
			return false;
		}
		else
		{
			return true;
		}
	}

	/**
	 * [get_model_all 所有模型]
	 * @return [type] [description]
	 */
	public function get_model_all()
	{
		return $this->model;
	}

	/**
	 * [add_model 添加模型]
	 */
	public function add_model()
	{

		$name = I('post.name');
		$remark = I('post.remark');
		// 创建表
		$sql = file_get_contents(BASE_LIB_PATH."Data/table.sql");
		$sql = preg_replace(array('/thinkcms_article_data/','/文章附表/'), array(C('DB_PREFIX')."article_".$name,$remark), $sql);
		$this->query($sql);

		$mid = $this->add();


		// 添加字段
		$data = array(
			array(
				'model_mid'=>$mid,
				'fname'=>'article_aid',
				'title'=>'关联字段',
				'validate'=>'',
				'require'=>0,
				'show_type'=>1,
				'show_lists'=>0,
				'is_system'=>1,
				'is_disabled'=>0,
				'sort'=>1,
			),
			array(
				'model_mid'=>$mid,
				'fname'=>'body',
				'title'=>'详细内容',
				'validate'=>'',
				'require'=>0,
				'show_type'=>3,
				'show_lists'=>0,
				'is_system'=>0,
				'is_disabled'=>0,
				'sort'=>900,
			)

		);
		$fieldModel = D('Field');
		foreach($data as $v)
		{
			$fieldModel->add_model_field($v);
		}
	
		$this->update_cache();
	}
	/**
	 * [edit_model 编辑模型]
	 * @return [type] [description]
	 */
	public function edit_model()
	{
		//旧表名
		$mid = I('post.mid',null,'intval');
		$oldename = $this->where(array('mid'=>$mid))->getField('name');
		$name = I('post.name');
		$remark = I('post.remark');
		// sql语句
		$sql = 'alter table '.C('DB_PREFIX').'article_'.$oldename."  rename ".C('DB_PREFIX').'article_'.$name;
		$this->query($sql);
		// 更改表注释
		$sql = 'alter table '.C('DB_PREFIX').'article_'.$name .' COMMENT="'.$remark.'"';
		$this->query($sql);

		$this->save();
		$this->update_cache();
	}


	/**
	 * [del_model 删除模型]
	 * @param  [type] $mid [description]
	 * @return [type]      [description]
	 */
	public function del_model($mid)
	{

		// 删除表
		$mid = I('get.mid',null,'intval');
		$name = $this->where(array('mid'=>$mid))->getField('name');
		$sql = 'drop table '.C('DB_PREFIX').'article_'.$name;
		$this->query($sql);

		// 删除字段
		D('Field')->del_model_field($mid);
		

		$this->delete($mid);
		$this->update_cache();
	}

	
	/**
	 * [find_one 单模型数据读取]
	 * @param  [type] $mid [description]
	 * @return [type]      [description]
	 */
	public function find_one($mid)
	{
		foreach($this->model as $k=>$v)
		{
			if($mid == $k)
				return $v;
		}
	}

	/**
	 * [get_field 获取指定信息]
	 * @param  [type] $mid       [description]
	 * @param  [type] $fieldName [description]
	 * @return [type]            [description]
	 */
	public function get_field($mid,$fieldName)
	{
		return $this->where(array('mid'=>$mid))->getField($fieldName);
	}
}